package com.weather.pm.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.widget.Toast;
import com.sina.weibo.sdk.api.ImageObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.IWeiboDownloadListener;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.weather.pm.R;
import com.weather.pm.volley.cache.BitmapLruCache;

public class WeiBoShareUtil {

	private static IWeiboShareAPI  mWeiboShareAPI = null;
	private static Context mContext;
	public static final String WEIBO_KEY = "2589211002";

	public WeiBoShareUtil() {
	}

	/**
	 * 娉ㄥ�?
	 * @param context 
	 * @author XieWenjun
	 */
	public static void registerToWX(Context context) {
		mContext = context;
		mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(context, WEIBO_KEY);
		mWeiboShareAPI.registerApp();
	}

	public static IWeiboShareAPI getInstance(Context context){
		if(mWeiboShareAPI == null){
			registerToWX(context);
		}
		return mWeiboShareAPI;
	}

	public static void sendMessage(String content,Bitmap bmp,String url) {

		if (!mWeiboShareAPI.isWeiboAppInstalled()) {
			mWeiboShareAPI.registerWeiboDownloadListener(new IWeiboDownloadListener() {
				@Override
				public void onCancel() {
					Toast.makeText(mContext, 
							R.string.weibosdk_demo_cancel_download_weibo, 
							Toast.LENGTH_SHORT).show();
				}
			});
		}

		if (mWeiboShareAPI.isWeiboAppSupportAPI()) {
			//int supportApi = mWeiboShareAPI.getWeiboAppSupportAPI();
			//if (supportApi >= 10351 /*ApiUtils.BUILD_INT_VER_2_2*/) {
			//sendMultiMessage(/*hasText, hasImage, */hasWebpage/*, hasMusic, hasVideo, hasVoice*/);
			//  } else {
			sendSingleMessage(content,bmp,url);
			//  }
		} else {
			Toast.makeText(mContext, R.string.weibosdk_demo_not_support_api_hint, Toast.LENGTH_SHORT).show();
		}
	}

	private static void sendSingleMessage(String content,Bitmap bmp,String url) {

		//WeiboMessage weiboMessage = new WeiboMessage();
		WeiboMultiMessage weiboMessage = new WeiboMultiMessage();
		/*if (hasText) {
            weiboMessage.mediaObject = getTextObj();
        }*/
		/* if (hasImage) {
            weiboMessage.mediaObject = getImageObj();
        }*/
		//weiboMessage.mediaObject =  getWebpageObj(title,imagepath, description, drawableId, url);
		weiboMessage.textObject = getTextObj(content+url);
		weiboMessage.imageObject = getImageObject(bmp);
		/* if (hasMusic) {
            weiboMessage.mediaObject = getMusicObj();
        }
        if (hasVideo) {
            weiboMessage.mediaObject = getVideoObj();
        }*/
		/*if (hasVoice) {
            weiboMessage.mediaObject = getVoiceObj();
        }*/

		SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
		//SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
		request.transaction = String.valueOf(System.currentTimeMillis());
		request.multiMessage = weiboMessage;
		mWeiboShareAPI.sendRequest(request);
	}

    private static TextObject getTextObj(String content) {
            TextObject textObject = new TextObject();
            textObject.text = content;
            return textObject;
    }
    
    private static ImageObject getImageObject(Bitmap bmp){
    	ImageObject imageObject = new ImageObject(); 
    	//String imagePath1 = new StringBuilder(imagePath.length() + 12).append("#W").append(0).append("#H").append(0).append(imagePath).toString();
    	//Bitmap bmp = new BitmapLruCache(mContext).getBitmap(imagePath1).copy(Config.RGB_565, true);
    	//int[] pics = IconUtils.getIconId(IconUtils.getIconName(imagePath));
		//Bitmap bmp = BitmapFactory.decodeResource(getResources(), pics[1]);
    	imageObject.setImageObject(bmp);
    	return imageObject;
    }
	
	private static int THUMB_SIZE = 150;
	/*private static WebpageObject getWebpageObj(String title,String imagePath,String description,int drawableId,String url) {
		
		WebpageObject mediaObject = new WebpageObject();
		mediaObject.identify = Utility.generateGUID();
		mediaObject.title = "title";
		mediaObject.defaultText = "defaultText";
		mediaObject.description = "description";

		imagePath = new StringBuilder(imagePath.length() + 12).append("#W").append(0).append("#H").append(0).append(imagePath).toString();
		Bitmap bmp = new BitmapLruCache(mContext).getBitmap(imagePath).copy(Config.RGB_565, true);
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE,THUMB_SIZE, true);
        bmp.recycle();
        mediaObject.thumbData = Util.bmpToByteArray(thumbBmp, true);
        
	//	mediaObject.setThumbImage(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.logo_application));
		mediaObject.actionUrl = "www.openart.cn";
		return mediaObject;

	}*/

}
